package com.mutouren.modules.datapush.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.modules.datapush.dao.RepeatStrategyDao;
import com.mutouren.modules.datapush.dao.impl.DaoFactoryImpl;
import com.mutouren.modules.datapush.strategy.RepeatStrategy;

public class RepeatStrategyConfig {
	
	private static RepeatStrategyDao repeatStrategyDao = DaoFactoryImpl.getInstance().getRepeatStrategyDao();
	private static Map<String, RepeatStrategy> mapRepeatStrategy = new HashMap<String, RepeatStrategy>();
	
	private final static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	private final static Logger runLogger = LogManager.getLogger(LogAlias.SystemRun.name());	
	
	static {
		load();
	}
	
	private RepeatStrategyConfig(){};
	
	private synchronized static void setConfig(Map<String, RepeatStrategy> map) {
		mapRepeatStrategy = map;
	}
	
	public static void load() {
		try {
			runLogger.info("RepeatStrategyConfig.load begin");
			Map<String, RepeatStrategy> map = new HashMap<String, RepeatStrategy>();
			
			List<RepeatStrategy> listStrategy = repeatStrategyDao.select(null);
			for(RepeatStrategy strategy : listStrategy) {
				map.put(strategy.getMethodAlias().trim(), strategy);
			}
			
			setConfig(map);
			runLogger.info("RepeatStrategyConfig.load end");
		} catch(Throwable t) {
			errorLogger.error("RepeatStrategyConfig.load error", t);
			throw t;
		}
	}	
	
	public static RepeatStrategy getRepeatStrategy(String methodAlias) {
		return mapRepeatStrategy.get(methodAlias);
	}
}
